﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace EzDeploy.Core
{
    public static class ConfigHelpers
    {
        public const string ConfigName = "config.txt";

        public static Config Load(string directoryName)
        {
            string configFileName = Path.Combine(directoryName, ConfigHelpers.ConfigName);
            Config config = null;

            if (File.Exists(configFileName))
            {
                try
                {
                    config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(configFileName));
                }
                catch (JsonSerializationException)
                {
                    
                }
                catch (JsonReaderException)
                {

                }
            }

            return config ?? (config = new Config());
        }

        public static void Save(string directoryName, Config config)
        {
            var serialized = JsonConvert.SerializeObject(config);
            File.WriteAllText(Path.Combine(directoryName, ConfigHelpers.ConfigName), serialized);
        }
    }
}
